﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using chihuopig.Web.Framework;
using Microsoft.AspNetCore.Http;
using chihuopig.IService;
using chihuopig.Web.verfiy;
using chihuopig.Tool;
using chihuopig.Service;
using chihuopig.DTO;

namespace chihuopig.Web.Admin.Controllers
{
  
    public class AdminlistController : AdminWattantyController
    {

        IHttpContextAccessor _iHttpContextAccessor;
        IAdminService _iAdminService;
        IRoleService _iRoleService;
        public AdminlistController
           (
               IHttpContextAccessor iHttpContextAccessor,
               IAdminService iAdminService,
               IRoleService iRoleService

           ) :base(iHttpContextAccessor) 
        {
            
            _iHttpContextAccessor = iHttpContextAccessor;
            _iAdminService = iAdminService;
            _iRoleService = iRoleService;
        }

        public async Task<IActionResult> adminadd()
        {
            var table=_iRoleService.GetTable();
            return View(await table);
        }
        
        public ActionResult testadd()
        {

            return Content("123877");
        }

        [HttpPost]
        public IActionResult adminadd(RoleAdminVefiy model)
        {
           // var table = _iRoleService.GetTable();
           string guid=  Guid.NewGuid().ToString();
            if (ModelState.IsValid)
            {
                try
                {
                    DBService.Sqlclient.Ado.BeginTran();
                    long Id= DBService.Sqlclient.Ado.SqlQuery<long>($" insert into Admin(Name,salt,password,Addtime) values('{model.username}','{guid}','{UtilityHelper.GetMd5(model.pass, guid)}',now()); select  @@IDENTITY").Single();
                    foreach (var item in model.RoleId)
                    {
                        DBService.Sqlclient.Ado.ExecuteCommand($" insert into AdminRole(AdminId,RoleId,Addtime) values({Id},{item},now());");
                    }
                    DBService.Sqlclient.Ado.CommitTran();
                    return Json(new JsonMsg() { succeed = true, msg = "添加成功" });
                }
                catch (Exception ex)
                {
                    DBService.Sqlclient.Ado.RollbackTran();
                    return  Json(new JsonMsg() { succeed = false, msg="添加失败" });
                }
                
            }
          
           return Json(ModelStateVefify.Vefifymsg(ModelState));
        }

        public async Task<IActionResult> adminedit(long id)
        {
            var resust= _iAdminService.EditAsync(id);
            var rolemode = _iRoleService.GetTable();
            var roleId = _iRoleService.GetAdminRoleId(id);
            ViewBag.AdminId = id;
            await Task.WhenAll();
            return View(new AdminRoleDTO { adminDTO= resust.Result, RoleListDTO=rolemode.Result, RoleId= roleId.Result});
        }
        [HttpPost]
        public async Task<IActionResult> adminedit(RoleAdminEditVefiy model)
        {
            if (ModelState.IsValid)
            {
                int i=  await _iAdminService.AdminRoleEdit(new AdminRoleEditDTO() { RoleId=model.RoleId, AdminId=model.AdminId, pass=model.pass});
                if (i>0)
                {
                    return Json(new JsonMsg() { succeed = true, msg = "添加成功" });
                }
                return Json(new JsonMsg() { succeed = false, msg = "添加失败" });

            }
            return Json(ModelStateVefify.Vefifymsg(ModelState));
        }

        [HttpPost]
        public async Task<IActionResult> delAll(long[] id)
        {
            var resust=await _iAdminService.delAll(id);
            if (resust > 0)
            {
                return Json(new { succeed = true, msg = "删除成功" });
            }
            return Json(new { succeed = false, msg = "删除失败" });
        }

        public async Task<IActionResult> List(int Pageindex=1,int PageSize=10)
        {
            var list=await _iAdminService.GetPage(Pageindex, PageSize);
            var total = await _iAdminService.GetPageTotal(PageSize);
            ViewBag.Pageindex = Pageindex;
            ViewBag.Start = Pageindex+5> total?1:Pageindex + 5;
            ViewBag.GetPageTotal = total+5> total? total:total + 5;
            return View(list);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(long Id)
        {
            var count=await  _iAdminService.Deleteable(Id);
            if (count>0)
            {
                return Json(new {succeed = true, msg ="删除成功"});
            }
            return Json(new {succeed=false,msg="删除失败"});
        }
    }
}